package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.storage.ContactSqlManager;
import com.yuntongxun.ecdemo.ui.contact.ECContacts;
import com.yuntongxun.ecdemo.voicechat.bean.UserInfo;
import com.yuntongxun.ecdemo.voicechat.util.InfoUtil;
import com.yuntongxun.ecdemo.voicechat.util.JsonHelper;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/12/14.
 */

public class GiftOpenActivity extends Activity {
    @InjectView(R.id.gift_img)
    ImageView giftImg;
    @InjectView(R.id.gift_from)
    TextView giftFrom;
    @InjectView(R.id.gift_name)
    TextView giftName;

    public static final String MESSAGE="message";
    public static final String INFO="info";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            /*set it to be no title*/
        requestWindowFeature(Window.FEATURE_NO_TITLE);
       /*set it to be full screen*/
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_gift_open);
        ButterKnife.inject(this);

        String msg=getIntent().getStringExtra(MESSAGE);
        Log.e("msg",msg);
        String url="";
        String name="";
        try {
            JSONObject object=new JSONObject(msg);
            url=object.getString("url");
            name=object.getString("name");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        giftName.setText(name);
        Picasso.with(this)
                .load(url)
                .placeholder(R.drawable.ic_loading)
                .tag(this)
                .into(giftImg);

        String infomation=getIntent().getStringExtra(INFO);
        String info="";
        ECContacts contact = ContactSqlManager.getContact(infomation);
        if( contact != null) {
            if(TextUtils.isEmpty(contact.getNickname())) {
                contact.setNickname(contact.getContactid());
            }
            info=contact.getNickname();

            if (contact.getNickname().contains("(")) {
                int index = info.indexOf("(");
                String message = info.substring(index + 1, info.length() - 1);

                UserInfo infos=null;
                try {
                    infos=parseResult(message);
                } catch (Exception e) {
                    e.printStackTrace();
                }



                if (infos!=null){
                    if (infos.getNickName().equals(InfoUtil.getLocalUserInfo(this).getNickName())){
                        giftFrom.setVisibility(View.INVISIBLE);
                        findViewById(R.id.gift_tips).setVisibility(View.INVISIBLE);
                    }else{
                        giftFrom.setText("From  "+infos.getNickName());
                    }

                }

            }
        } else {
            giftFrom.setText(info);
        }
    }

    public static UserInfo parseResult(String result) {
        JsonHelper<UserInfo> dataParser = new JsonHelper<UserInfo>(
                UserInfo.class);
        return dataParser.getData(
                result, "result");
    }

    @OnClick(R.id.gift_layout)
    public void onGiftLayoutClick(){
        finish();
    }


}
